package com.shd.edanpei.presenter;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.shd.edanpei.R;
import com.shd.edanpei.base.BasePresenterImpl;
import com.shd.edanpei.base.network.api.ApiCreator;
import com.shd.edanpei.base.network.exception.ResponeThrowable;
import com.shd.edanpei.base.network.rx.BaseObserver;
import com.shd.edanpei.base.network.rx.RxTransformer;
import com.shd.edanpei.bean.MessageModel;
import com.shd.edanpei.bean.PagingModel;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.presenter.contract.MessageContract;

import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by mc on 2018/3/29.
 * yang-wen-wen
 */
public class MessagePresenter extends BasePresenterImpl<MessageContract.MessageIView>
        implements MessageContract.MessageIPresenter {

    private int currentPage;
    private int totalPage;

    public MessagePresenter(MessageContract.MessageIView mView) {
        super(mView);
    }

    @Override
    public void refresh() {
        currentPage = 1;
        ApiCreator.getApi().messageList(currentPage, 2, DaoManager.getInstance().getUserBean().getRoleType())
                .compose(RxTransformer.<PagingModel<MessageModel>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<MessageModel>>() {
                    @Override
                    protected void onSuccess(PagingModel<MessageModel> data) {
                        if (data != null && data.getList().size() > 0) {
                            totalPage = data.getTotalPage();
                            mBaseIView.getAdapter().setNewData(data.getList());
                            if (currentPage >= totalPage) {
                                mBaseIView.getAdapter().loadMoreEnd();
                            } else {
                                mBaseIView.getAdapter().loadMoreComplete();
                            }
                            currentPage++;
                        } else {
                            mBaseIView.getAdapter().setNewData(null);
                            mBaseIView.getAdapter().setEmptyView(getNullLayout());
                        }
                    }


                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        mBaseIView.getAdapter().setEmptyView(getNullLayout());

                    }

                });
    }

    @Override
    public void loadMoe() {
        ApiCreator.getApi().messageList(currentPage, 1, DaoManager.getInstance().getUserBean().getRoleType())
                .compose(RxTransformer.<PagingModel<MessageModel>>errorHandle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<PagingModel<MessageModel>>() {
                    @Override
                    protected void onSuccess(PagingModel<MessageModel> data) {
                        if (data != null && data.getList().size() > 0) {
                            mBaseIView.getAdapter().addData(data.getList());
                            if (currentPage >= totalPage) {
                                mBaseIView.getAdapter().loadMoreEnd();
                            } else {
                                mBaseIView.getAdapter().loadMoreComplete();
                            }
                            currentPage++;
                        } else {
                            mBaseIView.getAdapter().loadMoreEnd();
                        }
                    }

                    @Override
                    public void onFailure(ResponeThrowable throwable) {
                        mBaseIView.getAdapter().loadMoreFail();
                    }
                });
    }

    private View getNullLayout() {
        View view = LayoutInflater.from(mBaseIView.getContext()).inflate(R.layout.null_layout, null);
        ImageView ivPic = view.findViewById(R.id.iv_null_icon);
        TextView tvHint = view.findViewById(R.id.tv_null_hint);
        ivPic.setImageResource(R.drawable.null_message);
        tvHint.setText("您还没有收到消息...");
        return view;
    }
}
